package com.mb.app.service.impl;

import com.mb.app.bo.ProductPvUvBO;
import com.mb.app.dao.IProductPvUvMapper;
import com.mb.app.dao.TbReportProductPvuvMapper;
import com.mb.app.domain.TbReportProductPvuv;
import com.mb.app.service.IProductPvUvService;
import com.mb.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 产品PVUV跑批服务实现
 *
 * @author
 * @create 2019-03-26 7:27 PM
 **/
@Service("productPvUvService")
@Slf4j
public class ProductPvUvServiceImpl implements IProductPvUvService {

    @Autowired
    private IProductPvUvMapper productPvUvMapper;

    @Autowired
    private TbReportProductPvuvMapper reportProductPvuvMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int runProductPvUv() {
        String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");
        Map<String,Object> map = new HashMap<>();
        map.put("clickTime", reportDate);

        List<ProductPvUvBO> list = productPvUvMapper.getProductList();
        if(list != null && list.size() != 0){
            Iterator<ProductPvUvBO> it = list.iterator();
            while(it.hasNext()){
                ProductPvUvBO productPvUvBO = it.next();
                Long appId = productPvUvBO.getAppId();
                Long productId = productPvUvBO.getProductId();

                TbReportProductPvuv reportProductPvuv = new TbReportProductPvuv();
                reportProductPvuv.setReportDate(reportDate);
                reportProductPvuv.setAppId(appId);
                reportProductPvuv.setProductId(productId);
                productPvUvMapper.deleteProductPvUvReport(reportProductPvuv);

                map.put("productId",productId);
                map.put("appId",appId);
                Integer pv = productPvUvMapper.productPvUv(map);
                reportProductPvuv.setPv(pv);

                Integer uv = this.getUv(reportDate,appId,productId);
                Integer oldUv = this.getOldUv(reportDate,appId,productId);
                reportProductPvuv.setUv(uv);
                reportProductPvuv.setOldUv(oldUv);
                reportProductPvuv.setReportDate(reportDate);
                reportProductPvuvMapper.insert(reportProductPvuv);
            }
        }
        return 0;
    }


    private Integer getUv(String reportDate,Long appId,Long productId){
        Integer uv = 0;
        Map<String,Object> map = new HashMap<>();
        map.put("reportDate", reportDate);
        map.put("appId", appId);
        map.put("productId", productId);
        uv = productPvUvMapper.getUv(map);
        return uv;
    }

    private Integer getOldUv(String reportDate,Long appId,Long productId){
        Integer oldUv = 0;
        Map<String,Object> map = new HashMap<>();
        map.put("appId", appId);
        map.put("productId", productId);
        map.put("reportDate", reportDate);
        oldUv = productPvUvMapper.getOldUv(map);
        return oldUv;
    }

}
